<template>
  <a-modal :open="open" width="750px" title="新增仪器" centered @ok="onOk" @cancel="onCancel">
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="仪器编号" class="w-1/2">
        <a-input v-model:value="formState.instrumentCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="仪器名称" class="w-1/2">
        <a-input v-model:value="formState.instrumentName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="起始样本号" class="w-1/2">
        <a-input v-model:value="formState.startNo" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="顺序号" class="w-1/2">
        <a-input-number v-model:value="formState.outorder" class="w-full" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="通讯方式" class="w-1/2">
        <a-radio-group v-model:value="formState.commType">
          <a-radio :value="0">串口</a-radio>
          <a-radio :value="1">TCPIP监听端口</a-radio>
        </a-radio-group>
      </a-form-item>
      <template v-if="!formState.commType">
        <a-form-item label="COM口" class="w-1/2">
          <a-select v-model:value="formState.comk" placeholder="请选择">
            <a-select-option value="COM1">COM1</a-select-option>
            <a-select-option value="COM2">COM2</a-select-option>
            <a-select-option value="COM3">COM3</a-select-option>
            <a-select-option value="COM4">COM4</a-select-option>
            <a-select-option value="COM5">COM5</a-select-option>
            <a-select-option value="COM6">COM6</a-select-option>
            <a-select-option value="COM7">COM7</a-select-option>
            <a-select-option value="COM8">COM8</a-select-option>
            <a-select-option value="COM9">COM9</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="波特率" class="w-1/2">
          <SelectItem v-model:value="formState.baudRate" type="DT00254" placeholder="请选择" allow-clear />
        </a-form-item>
        <a-form-item label="检验位" class="w-1/2">
          <SelectItem v-model:value="formState.checkBit" type="DT00255" placeholder="请选择" allow-clear />
        </a-form-item>
        <a-form-item label="数据位" class="w-1/2">
          <SelectItem v-model:value="formState.dataBit" type="DT00256" placeholder="请选择" allow-clear />
        </a-form-item>
        <a-form-item label="停止位" class="w-1/2">
          <SelectItem v-model:value="formState.stopBit" type="DT00257" placeholder="请选择" allow-clear />
        </a-form-item>
      </template>
      <template v-else>
        <a-form-item label="端口" class="w-1/2">
          <a-input v-model:value="formState.port" placeholder="请输入" />
        </a-form-item>
      </template>
    </a-form>
  </a-modal>
</template>
<script setup>
import { nextTick, ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SelectItem from '@/components/SelectItem'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({})

const onOk = async () => {
  const { code, msg } = await post('/lis/InstrumentType/doSaveInstrument', formState.value)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      nextTick(() => {
        formState.value = { ...props.rowData, commType: props.rowData.commType ?? 0, comk: null }
      })
    }
  },
  {
    immediate: true
  }
)
</script>
